package cn.newgrand.netcall.imp.advertisement;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jivesoftware.resource.Default;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.util.BrowserLauncher;
import org.jivesoftware.spark.util.GraphicUtils;

import cn.newgrand.netcall.advertisement.AdXML;
import cn.newgrand.netcall.advertisement.Advertisement;
import cn.newgrand.netcall.advertisement.AdvertisementPanel;
/**
 *
 * 聊天窗体的顶部广告类
 * 
 * @author SidneyRen
 *
 */
public class AdChatTop extends AdvertisementPanel {
	private List<Advertisement> adList = AdXML.getInstance().getAdChatTopList();
	private static int adNum = 0;
	private Image backgroundImage;
	
	public AdChatTop() {
		loadAd();

		add(getAdLabel(), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(1, 0, 1, 6), 0, 0));
	}
	
	/**
	 *  更换聊天窗口头部广告图片
	 *  并调用父类中的fillAd方法，设置ad、刷新面板
	 * 
	 * @param ad
	 */

	public void fillChatTopAd(Advertisement ad){
		if(null==ad.getImageIcon()) return;
		getAdLabel().setIcon(ad.getImageIcon());
		fillAd(ad);
	}

	public void loadAd() {
		if (adList.size() == 0)
			return;

		if (adList.size() > adNum)
			fillChatTopAd(adList.get(adNum));
		else{
			adNum = 0;
			fillChatTopAd(adList.get(adNum));
		}

	}

	
	/**
	 * 更新广告
	 * 
	 */
	public void updateAd() {
		System.out.println("updateAd");
		if(adList.size()==0)
			return;
		if (adList.size() > adNum) {
			fillChatTopAd(adList.get(adNum));
			adNum += 1;
		} else {
			adNum = 0;
			fillChatTopAd(adList.get(adNum));
		}
		
	}


}
